const BaseMapper = require("../../library/base-mapper");
const Model = require("../model/config");
const {Op} = require("sequelize");
module.exports = class ConfigMapper extends BaseMapper {
    constructor() {
        super();
        this.model = Model
    }


    async configs(key = null) {
        if (key) {
            let ret = await this.model.findOne({
                attributes: ['value']
                , where: {
                    key: key
                }
            })
            return ret ? ret.value : null
        }

        let list = await this.model.findAll({
            attributes: ['key', 'value']
        })
        let map = {}
        for(let item of list){
            map[item['key']] = item['value']
        }
        return map
    }

    async saveAll(data){
        for(let key in data){
            let item = await this.model.findOne({where: {key: key}})
            if(item){
                item.update({value: data[key]})
            }else{
                await this.model.create({
                    key:key,
                    value: data[key]
                })
            }
        }

    }
}
